package com.yunjian.core.api;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yunjian.common.utils.DateUtils;
import com.yunjian.core.entity.MessageCenter;
import com.yunjian.core.service.IMessageCenterService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import com.yunjian.common.utils.Constant;
import com.yunjian.common.utils.JsonUtil;
import com.yunjian.common.utils.StringUtil;
import com.yunjian.core.dto.ResponseDto;
import com.yunjian.core.service.JPushService;

@RestController
@RequestMapping("/app/push")
public class JPushController {

    private Logger logger = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private JPushService jPushServiceImpl;

    @Autowired
    private IMessageCenterService messageCenterServiceImpl;

    /**
     * 模拟极光推送
     *
     * @param param
     * @return
     */
    @RequestMapping(value = "/pushDefaultMsg", method = RequestMethod.POST)
    @ResponseBody
    public ResponseDto pushDefaultMsg(@RequestBody Map<String, Object> param) {
        logger.info("模拟极光推送{}", JsonUtil.toJsonString(param));
        List<String> aliasList = Arrays.asList(StringUtil.obj2String(param.get("email")));
        ResponseDto response = new ResponseDto(Constant.SUCCESS_CODE, null, Constant.SUCCESS_MESSAGE);

        MessageCenter message = messageCenterServiceImpl.getOne(new QueryWrapper<MessageCenter>().eq("id", 1242));

        Map<String, String> map = new HashMap<>();
        map.put("id", 1242 + "");
        map.put("title", message.getTitle());
        map.put("content", message.getContent());
        map.put("createTime", DateUtils.format(message.getCreateTime(), DateUtils.DATE_TIME_PATTERN));
        jPushServiceImpl.pushAline(aliasList, message.getTitle(), message.getContent(), map);
        response.setData(map);

        return response;
    }

}
